<?php
/**
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file license.txt or that can be found at 
 * http://code.google.com/p/billboardce/wiki/License.
 *
 * @package    Slide_IndexController
 * @category   Controller
 * @license    http://code.google.com/p/billboardce/wiki/License New BSD License
 * @version    SVN: $Id: $
 */

/**
 * Main controller for the application
 *
 * @package    Slide_IndexController
 * @category   Controller
 */
class Slide_IndexController extends Zend_Controller_Action 
{   
          
    /**
     * Shows the slides that the user has created
     *
     */
    public function indexAction()
    {
        // @todo right now this lists all slides.  Should just list the ones the user has created
        
        $this->view->acl = array(
                               'details' => $this->_helper->hasAccess('details'),
                               'add'     => $this->_helper->hasAccess('add'),
                               'edit'    => $this->_helper->hasAccess('edit'),
                               'delete'  => $this->_helper->hasAccess('delete')
                           );
        
        $slide = new Slide();
        
        $this->view->slides = $slide->fetchAll(null, 'title');
        
        $this->_helper->pageTitle('slide-index-index:title');
    }
    
    /**
     * Returns the slide info to the slidestream editor
     */
    public function getSlideInfoAction()
    {
        
        $get = Zend_Registry::get('getFilter');
        
        if (!isset($get->slideId)) {
            throw new Ot_Exception_Input('No slide ID provided');
        }
        
        $slide = new Slide();
        
        $thisSlide = $slide->find($get->slideId);
        
        if (is_null($thisSlide)) {
            throw new Ot_Exception_Input('Slide not found');
        }
        
        $this->_helper->viewRenderer->setNeverRender();
        $this->_helper->layout->disableLayout();
        
        echo Zend_Json::encode($thisSlide->toArray());
    }
    
    /**
     * Lets the user create a new slide.  If this is in an AJAX context, then 
     * we'll expect a slide stream ID to be given that the slide should also be
     * added to and we'll disable the layout.
     */
    public function addAction()
    {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNeverRender();
        
        if ($this->_request->isPost()) {
            
            $post = Zend_Registry::get('postFilter');
    
            if (!isset($post->slideStreamId) && empty($post->slideStreamId)) {
                echo Zend_Json::encode(array('rc' => 0, 'msg' => 'Error adding slide.  No slidestream ID was given. Please try again'));
                return;
            }
            
            if (!isset($post->slideType) && empty($post->slideType)) {
                echo Zend_Json::encode(array('rc' => 0, 'msg' => 'Error adding slide.  No slide type was given. Please try again'));
                return;                
            }
                
            $slide = new Slide();
            
            $data = array(
                        'slideType' => $post->slideType,
                        'title'     => 'New Slide'
                    );

            $db = $slide->getAdapter();
            $db->beginTransaction();
            
            try {
                $slideId = $slide->insert($data);
            } catch (Exception $e) {
                $db->rollBack();
                echo Zend_Json::encode(array('rc' => 0, 'msg' => 'Error adding slide, please try again'));
                return;                
            }
            
            $slideStreamMap = new Slide_Stream_Map();
                
            $data = array(
                        'slideStreamId' => $post->slideStreamId,
                        'entityType'    => 'slide',
                        'entityTypeId'  => $slideId,
                        'order'         => 1
                    );

            try {
                $slideStreamMap->insert($data);
            } catch (Exception $e) {
                $db->rollBack();
                echo Zend_Json::encode(array('rc' => 0, 'msg' => 'Error adding slide, please try again'));
                return;                
            }
            
            $db->commit();
            
            echo Zend_Json::encode(array(
                                        'rc'        => 1,
                                        'msg'       => 'Slide added successfully',
                                        'slideId'   => $slideId,
                                        'slideType' => $post->slideType
                                   ));
            return;
        }
    }
}